/**
 * 
 */
$(document).ready(function() {
	$('#buttonsPanel a').button();
	
	$('#waitMsg').dialog({
		modal: true
	});
	$('#buttonsPanel a').click(function() {
		var commandName = $(this).attr('id');
		$.post('action', { 'command': commandName, 'params': null}, function(data) {
			
		});
	});
	$('#takecards').unbind('click').click(function() {
		var commandName = $(this).attr('id');
		$.post('/table/action', { 'command': commandName, 'params': null}, function(data) {
			$('#cards').html(data).waitForImages(function() {
				$('#cards span img').addClass 'unchoosen'
				$('#cards span img').click(function() {
					$(this).toggleClass('unchoosen').toggleClass('choosen');
				});
			});
		});
	});
	$('#changecards').unbind('click').click(function() {
		var commandName = $(this).attr('id');
		var paramsS = '';
		var delimitter = ';';
		$('#cards img.choosen').each(function() {
			paramsS = paramsS.concat($(this).parent().attr('id'), ';');
		});
		$.post('/table/action', { 'command': commandName, 'params': paramsS}, function(data) {
			$('#cards').html(data);
		});
	});
	
	setInterval(function() {
		var currentState = parseInt($('#stId').attr('value'));
		$.get('/table/play/gms', function(data) {
			var tmpVal = parseInt(data);
			if(tmpVal > currentState) {
				location.reload(true);
			}
		});
	}, 1000);
});